import {Command} from '../constant';
import {IMainReducer} from '../types';
import {Action} from 'typings';
import produce from 'immer';
import * as immerUtil from '@/redux/immer-util';

const INITIAL_STATE: IMainReducer = {
  isReady: false,
  isLoading: false,
  status: {
    loadingGameInfo: false,
    loadingGameDaily: false,
    loadingUser: false,
    loadingGameTimes: false,
    loadingTask: false, // 查询任务列表
    loadingIsNewComer: false, // 是否 是新人保障
    loadingRanking: false,
    loadingProp: false, // 道具
    loadingTaskDetail: false, // 查询任务详情
    computingTask: false,
    loadingUserComputedGameTimes: false,
    loadingGameGrticle: false, // 查询游戏物品信息
    usingPropPossibility: false,
    givingGameTimes: false,
    loadingGameDailyTimes: false,
  },
  response: {
    isNewComer: false, // 是否 是新人保障
    loadGameInfo: '',
    loadGameDaily: '',
    loadUser: '',
    loadGameTimes: '',
    loadTask: '',
    loadRank: '',
    loadProp: '',
    loadTaskDetail: '', // 查询任务详情
    computedTask: '',
    loadUserComputedGameTimes: '', // 用户已用次数查询
    loadGameGrticle: '', // 查询游戏物品信息
    usePropPossibility: '', // 判断用户是否能使用道具
    giveGameTimes: '',
    loadGameDailyTimes: '',
  },
  userInfo: {
    isNewComer: '', // 是否是新人保障
    gameAvailableTimes: 0, // 游戏次数
    magicBean: 0, // 魔力豆
    gameComputedTimes: 0, // 已用游戏次数
  },
  gameInfo: {}, // 游戏基本信息
  gameReceiveInfo: {}, // 每日游戏收下

  // 任务列表
  task: {
    items: [],
    page: {
      total: 0,
      pageNum: 0,
      pageSize: 10,
    },
    detail: {},
  },

  rankingItems: [],
  rankingTopThreeItems: [], // 前三
  rankingTopElseItems: [], // 中间
  rankingSingledItems: [], // 未上榜

  // 道具列表
  prop: {
    items: [],
    page: {
      total: 0,
      pageNum: 0,
      pageSize: 10,
    },
  },
  codeMessage: '',
  gameCrticleInfo: {}, // 游戏物品信息
};

export default function main(state = INITIAL_STATE, action: Action): IMainReducer {
  const {type, payload} = action;

  return produce<IMainReducer>(state, (draftState) => {
    switch (type) {
      //通用改变数据
      case Command.commonChange:
        return immerUtil.commonChange(draftState, {...payload, key: 'main'});

      //初始化
      case Command.init:
        draftState.isReady = true;
        for (let propKey in payload.main) {
          draftState[propKey] = payload.main[propKey];
        }
        return draftState;

      //重置
      case Command.clean:
        for (let propKey in INITIAL_STATE) {
          draftState[propKey] = INITIAL_STATE[propKey];
        }
        return draftState;
    }
  });
}
